﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IdentityModel.Policy;
using System.IdentityModel.Claims;
using System.Xml.Linq;
using System.Security.Principal;

namespace RentAWorker.Business.WebHost.Code
{
    public class RentAWorkerPolicy : IAuthorizationPolicy
    {
        #region IAuthorizationPolicy Members

        public bool Evaluate(EvaluationContext evaluationContext, ref object state)
        {
            // Get the current user
            List<IIdentity> Identities =
                (List<IIdentity>)evaluationContext.Properties["Identities"];
            IIdentity CurrentUser = Identities[0];

            // Get the roles with the current identity
            string[] Roles = RentAWorkerGroupHelper.GetRolesForCurrentUser(CurrentUser);

            // Create and associate the custom principal
            evaluationContext.Properties["Principal"] = new GenericPrincipal(CurrentUser, Roles);

            return true;
        }

        public System.IdentityModel.Claims.ClaimSet Issuer
        {
            get { return ClaimSet.System; }
        }

        #endregion

        #region IAuthorizationComponent Members

        private Guid _Id = Guid.NewGuid();
        public string Id
        {
            get { return _Id.ToString(); }
        }

        #endregion
    }
}
